package chapter3.homework;

import java.io.*;

/**
 * @Project ：learn_java
 * @File ：Q6.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/3 19:13
 * @Description ：For study.
 */

public class Q6 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String path = "testIO\\message.dat";

        File file = new File(path);

        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))
        ) {
            Message message = new Message("张三", "李四", "你好", "today");
            oos.writeObject(message);
            System.out.println("写入对象文件成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        ) {
            Object obj = ois.readObject();

            Message m = (Message) obj;

            System.out.println(m.toString());
            System.out.println(m);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
